﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;

using ProjectBase.Core;

namespace Survey.Entity.Services
{
    [Serializable]
    public class Language : Persistent<int>
    {
        #region members
        CultureInfo _cultureInfo;
        #endregion members

        /// <summary>
        ///     The ISO name of the language.
        ///     Its value is stored and in runtime the CultureInfo based on this name
        ///     is instantiated
        /// </summary>
        public virtual string LanguageName { get; set; }

        public virtual bool ShowFlag { get; set; }

        #region virtual
        /// <summary>
        ///     Provides access to .NET built in information about selected language and culture
        /// </summary>
        protected virtual CultureInfo CultureInfo
        {
            get
            {
                if (_cultureInfo.IsNull())
                {
                    if (LanguageName.IsNotEmpty()
                        && CultureInfo.GetCultures(CultureTypes.AllCultures)
                            .Any(ci => ci.Name.Equals(LanguageName, StringComparison.Ordinal)))
                    {
                        _cultureInfo = new CultureInfo(LanguageName);
                    }
                }
                return _cultureInfo;
            }
        }

        /// <summary>
        ///     English name for this Language (English, German, Czech)
        /// </summary>
        public virtual string EnglishName
        {
            get
            {
                return CultureInfo.Is()
                           ? CultureInfo.EnglishName
                           : string.Empty;
            }
        }

        /// <summary>
        ///     Localized name for this Language (English, Deutsch, Čeština etc.)
        /// </summary>
        public virtual string NativeName
        {
            get
            {
                return CultureInfo.Is()
                           ? CultureInfo.NativeName
                           : string.Empty;
            }
        }
        #endregion virtual

        #region override abstract
        public override string ToDisplay()
        {
            return LanguageName + " - " + EnglishName;
        }

        protected override string GetDomainObjectSignature()
        {
            return LanguageName;
        }
        #endregion override abstract
    }
}